#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'HaiFeng'
__mtime__ = '2016/8/16'
"""
from py_at.Data import Data
from py_at.Bar import Bar
import talib
from py_at.strategy import Strategy
class rrTourdous(Strategy):
    """继承数据完成策略"""

    def __init__(self,cfg):

        super().__init__(cfg)
        # 初始化默认参数
        if cfg=="":
            self.p_ma1 = self.Params['MA1'] = 200
            self.p_lots = self.Params['Lots'] = 1
            self.Slippage = self.Params['Slippage'] = 1
        else:
            self.p_ma1 = self.Params['MA1']
            self.p_lots = self.Params['Lots']
            self.Slippage = self.Params['Slippage']
            # 图表指标显示
            self.IndexDict['upline'] = self.Upline = []
            self.IndexDict['dowline'] = self.Dowline = []
            self.IndexDict['ma'] = self.ma = []





        self.LowAfterEntry=0 #开仓之后经过的高点
        self.HighAfgerEntry=0 #开仓之后经过的低点
        self.timeD=''
        self.MovePrice=0

    def OnBarUpdate(self, data=Data, bar=Bar):

        if len(self.C) < self.p_ma1:     # 如果bar数目小于 p._ma2参数  退出
            return

        SellEntry=False
        SellExit=False
        BuyEntry=False
        BuyExit=False
        if (self.timeD != self.D[-1]):
            if len(self.Upline)==0:    #填充策略初始化部分的数据  都填写为None
                for i in range(self.p_ma1):
                    self.Upline.append(None)
                    self.Dowline.append(None)
                    self.ma.append(None)
            else:

                Dayopen = self.OpenD[0]

                Band = self.HighD[1] - self.LowD[1]
                UpLine = Dayopen + Band
                LowLine = Dayopen - Band
                ma1 = talib.MA(self.C, self.p_ma1)

                self.timeD = self.D[-1]
                self.Upline.append(UpLine)
                self.Dowline.append(LowLine)
                self.ma.append(ma1[-1])
                if self.Dowline[-2]!=None:   #保证-2能取到值
                    SellEntry = self.C[-2] < self.Dowline[-2] and self.Dowline[-2] <= ma1[-2]
                    SellExit = self.C[-2] > self.Upline[-2] or self.C[-2] > ma1[-2]
                    BuyEntry = self.C[-2] > self.Upline[-2] and self.Upline[-2] > ma1[-2]
                    BuyExit = self.C[-2] < self.Dowline[-2] or self.C[-2] < ma1[-2]
                else:
                    return

        if(self.Position==0 and SellEntry):
            self.SellShort(self.O[-1]-self.MovePrice,self.p_lots,'卖开')
            #print('卖开%s %s ' % (self.D[-1],self.O[-1]-self.MovePrice))
        if(self.Position==0 and BuyEntry):
            self.Buy(self.O[-1]+self.MovePrice,self.p_lots,'买开')
            #print('买开%s %s ' % (self.D[-1], self.O[-1]+self.MovePrice))
        if(self.Position<0 and SellExit):
            self.BuyToCover(self.O[-1]+self.MovePrice,self.p_lots,'买平')
            #print('买平%s %s ' % (self.D[-1],self.O[-1]+self.MovePrice))
        if(self.Position>0 and BuyExit):
            self.Sell(self.O[-1]-self.MovePrice,self.p_lots,"卖平")
            #print('卖平%s %s ' % (self.D[-1], self.O[-1]-self.MovePrice))



        if(self.Position<0 and self.LastEntryDateShort!=self.D[-1] and self.LowAfterEntry<=self.LastEntryPriceShort*(1-0.01*20) and self.H[-1]>=self.LowAfterEntry+(self.LastEntryPriceShort-self.LowAfterEntry)*0.01*20):
            self.BuyToCover(round(self.LowAfterEntry+(self.LastEntryPriceShort-self.LowAfterEntry[-2])*0.01*20)+self.MovePrice,self.p_lots,'买平')
            #print('空头盈利达到百分%20之后回撤百分%20止损')
        if(self.Position>0 and self.LastEntryDateLong!=self.D[-1] and self.HighAfgerEntry>=self.LastEntryPriceLong*(1+0.01*20) and self.L[-1]<=self.HighAfgerEntry-(self.HighAfgerEntry-self.LastEntryPriceLong)*0.01*20):
            self.Sell(round(self.HighAfgerEntry[-2]-(self.HighAfgerEntry[-2]-self.LastEntryPriceLong)*0.01*20)-self.MovePrice,self.p_lots,'卖平')
            #print('多头盈利达到百分%20之后回撤百分%20止损')


        if(self.Position < 0 and self.LastEntryDateShort != self.D[-1] and self.LowAfterEntry <= self.LastEntryPriceShort * (1 - 0.01 * 10) and self.H[-1] >= self.LowAfterEntry + (self.LastEntryPriceShort - self.LowAfterEntry) * 0.01 * 50):
            self.BuyToCover(round(self.LowAfterEntry + (self.LastEntryPriceShort - self.LowAfterEntry) * 0.01 * 50)+self.MovePrice,self.p_lots, '买平')
            #print('空头盈利达到百分%10之后回撤百分%50止损')
        if(self.Position > 0 and self.LastEntryDateLong != self.D[-1] and self.HighAfgerEntry >= self.LastEntryPriceLong * (1 + 0.01 * 10) and self.L[-1] <= self.HighAfgerEntry - (self.HighAfgerEntry - self.LastEntryPriceLong) * 0.01 * 50):
            self.Sell(round(self.HighAfgerEntry - (self.HighAfgerEntry - self.LastEntryPriceLong) * 0.01 * 50)-self.MovePrice,self.p_lots, '卖平')
            #print('多头盈利达到百分%10之后回撤百分%50止损')


        if (self.Position < 0 and self.LastEntryDateShort != self.D[-1] and self.LowAfterEntry <= self.LastEntryPriceShort * (1 - 0.01 * 5) and self.H[-1] >= self.LowAfterEntry + (self.LastEntryPriceShort - self.LowAfterEntry) * 0.01 * 80):
            self.BuyToCover(round(self.LowAfterEntry + (self.LastEntryPriceShort - self.LowAfterEntry) * 0.01 * 80)+self.MovePrice,
                            self.p_lots, '买平')
            #print('空头盈利达到百分%5之后回撤百分%80止损')
        if (self.Position > 0 and self.LastEntryDateLong != self.D[-1] and self.HighAfgerEntry >= self.LastEntryPriceLong * (1 + 0.01 * 5) and self.L[-1] <= self.HighAfgerEntry - (self.HighAfgerEntry - self.LastEntryPriceLong) * 0.01 * 80):
            self.Sell(round(self.HighAfgerEntry - (self.HighAfgerEntry - self.LastEntryPriceLong) * 0.01 * 80)-self.MovePrice,
                      self.p_lots, '卖平')
            #print('多头盈利达到百分%5之后回撤百分%80止损')


        if(self.Position<0 and self.H[-1]>=self.LastEntryPriceShort*(1+1*0.03)):
            self.BuyToCover(round(self.LastEntryPriceShort*(1+1*0.03))+self.MovePrice,self.p_lots,'买平')
        if(self.Position>0 and self.L[-1]<=self.LastEntryPriceLong*(1-1*0.03)):
            self.Sell(round(self.LastEntryPriceLong*(1-1*0.03))-self.MovePrice,self.p_lots,'卖平')

        #记录开仓的高低点
        if self.EntryDateLong==self.D[-1] or self.EntryDateShort==self.D[-1]:
            self.HighAfgerEntry = self.H[-1]
            self.LowAfterEntry = self.L[-1]
        else:
            self.HighAfgerEntry = max(self.HighAfgerEntry, self.H[-2])
            self.LowAfterEntry = min(self.LowAfterEntry, self.L[-2])




